﻿//
// *************************************************************************************
// WP-Framework
// 
// Developed by
// Lukas Kretschmar (lkretschmar.dev@bluewin.ch)
//
// Switzerland, Copyright (c) 2013 lkretschmar
// This content is released under the MIT License (http://opensource.org/licenses/MIT).
//     
// *************************************************************************************
//
//
			

using System.Reflection;
using KretschIT.WP_Fx.Core.Validation;
using System;
namespace KretschIT.WP_Fx.EasyMoq
{
    public static class MockTypeExtensions
    {
        public static PropertyInfo Property<T>(this T source, string propertyName)
        {
            It.IsNotNull(() => source);
            var property = typeof(T).GetProperty(propertyName);
            if (property == null)
            {
                throw new ArgumentException("Property does not exist.", "propertyName");
            }
            if (MustUsePropertyInfoFromInstance(property))
            {
                property = source.GetType().GetProperty(propertyName);
            }
            return property;
        }

        private static bool MustUsePropertyInfoFromInstance(PropertyInfo property)
        {
            return property.CanWrite && !property.CanRead && !property.DeclaringType.IsPublic;
        }
    }
}
